package com.topInterview;

/**
 * @Author: huangzhigao
 * @Date: 2022/2/20 14:24
 */
public class Leecode50_pow {
    public double myPow(double x, int n) {
        if (n == 0) {
            //任何数的0次方都为1
            return 1D;
        }

        if (n == Integer.MIN_VALUE) {
            return (x == 1D || x == -1D) ? 1D : 0;
        }

        int pow = Math.abs(n);
        double res = 1D;
        double t = x;
        while (pow != 0) {
            if ((pow & 1) != 0) {
                res = res * t;
            }
            pow = pow >> 1;
            t = t * t;

        }

        return n < 0 ? (1D / res) : res;

    }
}
